package com.fub.view.mapcomponents;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.Log;

import com.fub.guidance.Graph;
import com.fub.guidance.Point;
import com.fub.guidance.Relation;
import com.fub.guidance.Vertex;
import com.fub.util.Util;
import com.fub.util.PreferenceDAO;

public class RelationComponent {

	private Relation relation;
	private int color;
	private Paint paint;
	private Graph graph;
	private Point positionOnDisplay;
	private PreferenceDAO preferenceDAO;

	public RelationComponent(Relation relation, PreferenceDAO preferenceDAO,
			int color) {
		this.preferenceDAO = preferenceDAO;
		this.relation = relation;
		this.color = color;
		this.graph = Graph.getInstance();
		initPaints();
	}

	private void initPaints() {
		this.paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		this.paint.setStyle(Paint.Style.FILL);
		this.paint.setStrokeWidth(1);
		this.paint.setColor(this.color);
	}

	public void draw(Canvas canvas) {
		Path p = new Path();
		String fromId = this.relation.getNodes().get(0);
		Vertex from = this.graph.getNodeById(fromId);
		this.positionOnDisplay = Util.calculateZoomedPosition(from.getPosition(),
				this.preferenceDAO);
		p.moveTo(this.positionOnDisplay.getX(), this.positionOnDisplay.getY());
		for (int i = 1; i < this.relation.getNodes().size(); i++) {
			String nodeId = this.relation.getNodes().get(i);
			Vertex to = this.graph.getNodeById(nodeId);
			this.positionOnDisplay = Util.calculateZoomedPosition(
					to.getPosition(), this.preferenceDAO);
			p.lineTo(this.positionOnDisplay.getX(), this.positionOnDisplay.getY());
			from = to;
		}
		Log.d("Relation", "type:" + this.relation.getType() + " path " + p);
		canvas.drawPath(p, this.paint);
	}
}
